//
//  TollStationEventDetailController.m
//  Runone
//
//  Created by runone02 on 16/12/7.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "TollStationEventDetailController.h"
#import "RequestGinsengModel.h"
#import "RequestHelper.h"
#import "AppLocalManager.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "TollStationInfo.h"
#import "TollEventTopView.h"
#import "TollPicturesCell.h"
#import "UIImageView+WebCache.h"
#import "MJExtension.h"
#import "UIView+Addition.h"
#import "AppConstants.h"
#import "ROPhotoBrowser.h"
#import "TollStationHeaderView.h"

@interface TollStationEventDetailController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong,nonatomic)TollEvent *backModel;

@property (nonatomic, strong) NSMutableArray *allPicturesArray;

@property (nonatomic, strong) TollStationHeaderView *headerView;

@property (nonatomic, strong) UIImage *imageName;//当前点击的图片
@end

@implementation TollStationEventDetailController

#pragma   -----------收费事件详情


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _backModel = [[TollEvent alloc]init];
    self.title = @"收费事件";
  
    [self configNavigationbar];
    
    self.tableView.delegate       = self;
    self.tableView.dataSource     = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset   = UIEdgeInsetsMake(64, 0, 0, 0);
    self.tableView.bounces        = NO;
   
//    [self setHeaderData];
    [self loadData];
    
    _allPicturesArray = [[NSMutableArray alloc] init];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void) configNavigationbar {
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.size     = CGSizeMake(100, 30);
    titleLabel.text     = @"收费事件详情";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
}

- (void) configHeader {
    CGFloat headerHeigh ;
    headerHeigh = [self caculateHeaderViewHeight];
    _headerView            = [[[NSBundle mainBundle] loadNibNamed:@"TollStationHeaderView" owner:self options:nil] lastObject];
    _headerView.frame      = CGRectMake(0, 0, self.view.bounds.size.width,headerHeigh);
    _headerView.model      = _model;
    _headerView.backModel  = _backModel;
    self.tableView.tableHeaderView = _headerView;
}

- (CGFloat) caculateHeaderViewHeight {
    CGFloat dealDetailH = 0.0 ;
    CGSize maxSize   = CGSizeMake([UIScreen mainScreen].bounds.size.width - 80 - 16 - 8, MAXFLOAT);
    
    if (![_backModel.EventDescription isEqualToString:@""]) {
        dealDetailH = [_backModel.EventDescription boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
    }
    // 校准
    if (dealDetailH < 17) {
        dealDetailH = 30;
    }
    CGFloat headerHeight = 60 + dealDetailH ;
    return headerHeight;
}

- (void)loadData {//0逃费
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetTollStationEventDetailInfoModel=[[RequestGinsengModel alloc] init];
    GetTollStationEventDetailInfoModel.URL         = GetEventDataURL;
    GetTollStationEventDetailInfoModel.DataValue   = [self DataValueStr:self.model.EventUID];
    GetTollStationEventDetailInfoModel.UpdatedTime = @"";
    GetTollStationEventDetailInfoModel.SystemCode  = @"GuangleHighway";
    GetTollStationEventDetailInfoModel.MethodName  = @"GetTollStationEventDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        _backModel = [TollEvent mj_objectWithKeyValues:requestOutginseng.DataValue];
         [self configHeader];
        if (_backModel.PicturePath.count > 0) {
            self.tableView.scrollEnabled = YES;
            [self.tableView reloadData];
        }else {
            self.tableView.scrollEnabled = NO;
        }
            };
    [requestHelper RequestGinsengModel:GetTollStationEventDetailInfoModel andPushController:self];
}

-(NSString *)DataValueStr:(NSString *)IncidentUID {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"EventUID\",\"value\":\"%@\"}]",IncidentUID];
    return valueStr;
}
#pragma mark -delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TollPicturesCell *cell = [TollPicturesCell cellWithTableView:tableView];
    cell.picturesArray = _backModel.PicturePath;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  216 * _backModel.PicturePath.count - 8;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    //    UIView *imageView = tap.view;
//    ROPhotoBrowser *browser           = [[ROPhotoBrowser alloc] init];
//    browser.isAnnimation              = NO;
//    browser.currentImageIndex         = indexPath.row;
//    browser.sourceImagesContainerView = self.tableView;
//    browser.imageCount                = _backModel.PicturePath.count;
//    browser.delegate                  = self;
////    NSString *str = _backModel.PicturePath[indexPath.row];
////    _imageName = [UIImage imageNamed:str];
//    [browser show];
//    
//}

#pragma mark - ROPhotoBrowserDelegate
//- (NSURL *)photoBrowser:(ROPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
//    NSString *imageName = _backModel.PicturePath[index];
//    NSURL *url = [NSURL URLWithString:imageName];
//    return url;
//}
//
//- (UIImage *)photoBrowser:(ROPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
//
//    return _imageName;
//}

@end
